package com.cn.onetrip.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.objects.PointAudioObj;
import com.cn.onetrip.objects.RoutePointObj;
import com.cn.onetrip.untility.SysApplication;

public class PopupLayout extends RelativeLayout {

	public static final int POP_ANIM_END = 1;
	public static final int DIS_ANIM_END = 2;

	private Context context;
	private int subItemNum = 0;
	private Point ptPop;
	private Point ptTemp;
	private Point ptDest;
	private RelativeLayout layoutPopCenter;
	private ImageView imagePop;
	private RadialView imagePopBG;
	private TextView textTitle;
	private TextView textTime;

	private Point ptScreenCenter;

	private final int FIRST_QUADRANT = 0;
	private final int SECOND_QUADRANT = 1;
	private final int THIRD_QUADRANT = 2;
	private final int FOURTH_QUADRANT = 3;

	private final int POPUP_ANIM_TIME = 180;
	private final int DISAPPEAR_ANIM_TIME = 200;

	private int pointPos;
	private double degreeOld = 0;

	private RoutePointObj pointObj;
	private List<AudioPlayLayout> audioLayoutList;
	private int subItemIndex = 0;

	private Handler msgHandler;

	private boolean hasPopup = false;
	private boolean isAnimStart = false;
	private int popDistance = 200;
	private int audioDistance = 100;

	private Timer timer = null;
	private float bgRadius;

	public PopupLayout(Context context) {
		super(context);
		this.context = context;
		setWillNotDraw(false);
	}

	public PopupLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		setWillNotDraw(false);
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();

		layoutPopCenter = (RelativeLayout) findViewById(R.id.layout_pop_center);
		imagePop = (ImageView) findViewById(R.id.image_popup);
		imagePopBG = (RadialView) findViewById(R.id.image_popup_bg);
		textTitle = (TextView) findViewById(R.id.text_title);
		textTime = (TextView) findViewById(R.id.text_time);

		audioLayoutList = new ArrayList<AudioPlayLayout>();
	}

	public void setHandler(Handler handler) {
		msgHandler = handler;
	}

	public void setPoint(RoutePointObj pointObj, Point point) {

		this.pointObj = pointObj;
		subItemNum = pointObj.audioList.size();

		int drawableID = getResources().getIdentifier("other_dpp", "drawable",
				context.getPackageName());
		int colorID = getResources().getIdentifier("title_other_color",
				"color", context.getPackageName());
		if (pointObj.typeID == RoutePointObj.TYPE_IN_LINE) {
			int index = (pointObj.index + 1) % (SysApplication.POP_NUM / 2);
			drawableID = getResources().getIdentifier("dpp" + index,
					"drawable", context.getPackageName());
			colorID = getResources().getIdentifier("title_" + index + "_color",
					"color", context.getPackageName());
		}

		imagePop.setBackgroundResource(drawableID);
		textTitle.setTextColor(context.getResources().getColor(colorID));
		textTime.setTextColor(context.getResources().getColor(colorID));
		textTime.setVisibility(View.GONE);
		textTitle.setText(String.format("%s", pointObj.name));

		View v = ((Activity) context).getWindow().findViewById(
				Window.ID_ANDROID_CONTENT);

		ptScreenCenter = new Point(v.getWidth(), v.getHeight());

		int distance = v.getWidth() < v.getHeight() ? v.getWidth() : v
				.getHeight();
		popDistance = (int) (distance / 3.6);
		audioDistance = (int) (distance / 4);

		ptPop = point;
		ptTemp = point;
		degreeOld = 0;
		hasPopup = false;
		calculateDestPos();
	}

	// public void configurationChanged() {
	// View v = ((Activity) context).getWindow().findViewById(
	// Window.ID_ANDROID_CONTENT);
	// ptScreenCenter.x = v.getHeight();
	// ptScreenCenter.y = v.getWidth();
	//
	// invalidate();
	// }

	private void calculateDestPos() {

		double radian = Math.atan2(Math.abs(ptPop.y - ptScreenCenter.y),
				Math.abs(ptPop.x - ptScreenCenter.x));

		Point point = new Point();

		Drawable drawablePop = context.getResources().getDrawable(
				R.drawable.dpp1);
		int w = drawablePop.getIntrinsicWidth();
		int h = drawablePop.getIntrinsicHeight();

		// top of screen
		if (ptPop.y < ptScreenCenter.y) {
			if (ptPop.x > ptScreenCenter.x) {
				point.x = (int) (ptPop.x
						- Math.abs(popDistance * Math.cos(radian)) - w / 2);
				point.y = (int) (ptPop.y
						+ Math.abs(popDistance * Math.sin(radian)) - h / 2);

				pointPos = FIRST_QUADRANT;
			} else {
				point.x = (int) (ptPop.x
						+ Math.abs(popDistance * Math.cos(radian)) - w / 2);
				point.y = (int) (ptPop.y
						+ Math.abs(popDistance * Math.sin(radian)) - h / 2);

				pointPos = SECOND_QUADRANT;
			}

		} else { // bottom of screen
			if (ptPop.x > ptScreenCenter.x) {
				point.x = (int) (ptPop.x
						- Math.abs(popDistance * Math.cos(radian)) - w / 2);
				point.y = (int) (ptPop.y
						- Math.abs(popDistance * Math.sin(radian)) - h / 2);

				pointPos = FOURTH_QUADRANT;
			} else {
				point.x = (int) (ptPop.x
						+ Math.abs(popDistance * Math.cos(radian)) - w / 2);
				point.y = (int) (ptPop.y
						- Math.abs(popDistance * Math.sin(radian)) - h / 2);

				pointPos = THIRD_QUADRANT;
			}
		}

		ptDest = point;

		addSubItem(Math.toDegrees(radian));
	}

	private void addSubItem(double degree) {
		if (subItemNum == 0)
			return;

		double offsetDegree = 240 / subItemNum;
		if (subItemNum <= 3)
			offsetDegree = 180 / subItemNum;
		else if (subItemNum > 6)
			offsetDegree = 320 / subItemNum;

		double firstDegree = 0;
		double baseDegree = degree;
		if (subItemNum == 1)
			baseDegree = degree + offsetDegree / 2;
		else if (subItemNum == 2)
			baseDegree = (pointPos == FIRST_QUADRANT || pointPos == THIRD_QUADRANT) ? degree
					+ offsetDegree / 2
					: degree - offsetDegree / 2;
		switch (pointPos) {
		case FIRST_QUADRANT:
			firstDegree = baseDegree + 180 - (subItemNum / 2) * offsetDegree;
			break;

		case SECOND_QUADRANT:
			firstDegree = 360 - baseDegree - (subItemNum / 2) * offsetDegree;
			break;

		case THIRD_QUADRANT:
			firstDegree = baseDegree - (subItemNum / 2) * offsetDegree;
			break;

		case FOURTH_QUADRANT:
			firstDegree = 180 - baseDegree - (subItemNum / 2) * offsetDegree;
			break;

		default:
			break;
		}

		for (int i = 0; i < pointObj.audioList.size(); i++) {
			PointAudioObj itemData = pointObj.audioList.get(i);
			AudioPlayLayout layoutAudio = (AudioPlayLayout) View.inflate(
					context, R.layout.point_audio_view, null);

			layoutAudio.setId((int) (Math.random() * 100000));

			layoutAudio.setHandler(audioHandler);
			layoutAudio.setTitleVisible(View.GONE);

			layoutAudio.hasPopup = false;
			getItemStartEndPoint(layoutAudio, firstDegree + i * offsetDegree);
			layoutAudio.setAudioData(itemData, pointObj.index, pointObj.typeID);
			layoutAudio.degree = getActuralDegree(layoutAudio.ptEnd,
					layoutAudio.ptStart);

			audioLayoutList.add(layoutAudio);
		}
	}

	private void getItemStartEndPoint(AudioPlayLayout audioLayout, double degree) {

		Point ptStart = new Point();
		Point ptEnd = new Point();

		Drawable drawablePop = context.getResources().getDrawable(
				R.drawable.dpp1);
		int w = drawablePop.getIntrinsicWidth();
		int h = drawablePop.getIntrinsicHeight();

		ptStart.x = (int) (ptDest.x + w / 2);
		ptStart.y = (int) (ptDest.y + h / 2);

		degree = degree % 360;
		if (degree <= 0)
			degree = 360 + degree;

		double radian = Math.toRadians(degree);// 2*Math.PI/360*degree; //from

		if (degree >= 0 && degree < 90) {
			ptEnd.x = (int) (ptStart.x + Math.abs(audioDistance
					* Math.cos(radian)));
			ptEnd.y = (int) (ptStart.y - Math.abs(audioDistance
					* Math.sin(radian)));

		} else if (degree >= 90 && degree < 180) {
			ptEnd.x = (int) (ptStart.x - Math.abs(audioDistance
					* Math.cos(radian)));
			ptEnd.y = (int) (ptStart.y - Math.abs(audioDistance
					* Math.sin(radian)));

		} else if (degree >= 180 && degree < 270) {
			ptEnd.x = (int) (ptStart.x - Math.abs(audioDistance
					* Math.cos(radian)));
			ptEnd.y = (int) (ptStart.y + Math.abs(audioDistance
					* Math.sin(radian)));

		} else {
			ptEnd.x = (int) (ptStart.x + Math.abs(audioDistance
					* Math.cos(radian)));
			ptEnd.y = (int) (ptStart.y + Math.abs(audioDistance
					* Math.sin(radian)));
		}

		audioLayout.ptStart = ptStart;
		audioLayout.ptEnd = ptEnd;
	}

	private Handler audioHandler = new Handler() {

		public void dispatchMessage(android.os.Message msg) {
			if (audioLayoutList.size() <= 0) {
				return;
			}
			if (msg.what == AudioPlayLayout.START_PLAY) {
				for (int i = 0; i < audioLayoutList.size(); i++) {
					AudioPlayLayout layout = audioLayoutList.get(i);
					layout.isPauseing = false;
				}
				textTitle.setText(context.getResources().getString(
						R.string.audio_loading));
				imagePopBG.setVisibility(View.VISIBLE);
				if (timer != null)
					timer.cancel();
				popShining();
			} else if (msg.what == AudioPlayLayout.UPDATE_TIME) {
				textTime.setVisibility(View.VISIBLE);
				String restTime = (String) msg.obj;
				textTime.setText(restTime.split(",")[0]);
				textTitle.setText(restTime.split(",")[1]);
			} else if (msg.what == AudioPlayLayout.START_OTHER_AUDIO) {
				for (int i = 0; i < audioLayoutList.size(); i++) {
					AudioPlayLayout layout = audioLayoutList.get(i);
					layout.closeAudio();
				}
				if (timer != null)
					timer.cancel();

				textTime.setVisibility(View.GONE);
				int index = (Integer) msg.arg1;
				audioLayoutList.get(index).play();

			} else if (msg.what == AudioPlayLayout.STOP_PLAY) {
				textTitle.setText(String.format("%s", pointObj.name));
				textTime.setVisibility(View.GONE);
				timer.cancel();
				imagePopBG.setVisibility(View.GONE);
			} else if (msg.what == AudioPlayLayout.PAUSE_PLAY) {
				int index = (Integer) msg.arg1;
				AudioPlayLayout layout = audioLayoutList.get(index);
				if (!layout.isPlaying && !layout.isPrepered) {
					layout.closeAudio();
					textTitle.setText(String.format("%s", pointObj.name));
					textTime.setVisibility(View.GONE);
					if (timer != null)
						timer.cancel();
					imagePopBG.setVisibility(View.GONE);
					return;
				} else {
					textTime.setVisibility(View.VISIBLE);
				}
			} else if (msg.what == AudioPlayLayout.RESTART_PLAY) {
				imagePopBG.setVisibility(View.VISIBLE);
				textTime.setVisibility(View.VISIBLE);
				int index = (Integer) msg.arg1;
				audioLayoutList.get(index).reStartPlayAudio();
			}

		};
	};

	public void LockScreen() {
		if (audioLayoutList == null) {
			return;
		}
		for (int i = 0; i < audioLayoutList.size(); i++) {
			AudioPlayLayout layout = audioLayoutList.get(i);
			if (layout.isPlaying) {
				layout.pauseAudio();
			}
		}
	}

	public void popupAnimation() {

		Log.i("ptDest start", String.valueOf(ptDest));
		TranslateAnimation tAnimation = new TranslateAnimation(ptPop.x,
				ptDest.x, ptPop.y, ptDest.y);

		ScaleAnimation sAnimation = new ScaleAnimation(0, 1, 0, 1);

		AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(sAnimation);
		animationSet.addAnimation(tAnimation);
		animationSet.setDuration(POPUP_ANIM_TIME);

		layoutPopCenter.setAnimation(animationSet);
		animationSet.startNow();

		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {

				new Handler().post(new Runnable() {

					public void run() {

						hasPopup = true;
						layoutPopCenter.clearAnimation();
						RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT);
						lp.setMargins(ptDest.x, ptDest.y, 0, 0);
						layoutPopCenter.setLayoutParams(lp);
					}

				});

				subItemAnimation();
			}
		});

	}

	private void subItemAnimation() {

		if (subItemIndex == audioLayoutList.size()) {
			subItemIndex = 0;

			Message msg = new Message();
			msg.what = POP_ANIM_END;
			msgHandler.sendMessage(msg);

			return;
		}

		final AudioPlayLayout audioLayout = audioLayoutList.get(subItemIndex);
		audioLayout.clearAnimation();
		this.addView(audioLayout);

		Drawable drawable = context.getResources()
				.getDrawable(R.drawable.play1);
		TranslateAnimation tAnimation = new TranslateAnimation(
				audioLayout.ptStart.x, audioLayout.ptEnd.x
						- drawable.getIntrinsicWidth() / 2,
				audioLayout.ptStart.y, audioLayout.ptEnd.y
						- drawable.getIntrinsicHeight() / 2);

		ScaleAnimation sAnimation = new ScaleAnimation(0, 1, 0, 1);
		AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(sAnimation);
		animationSet.addAnimation(tAnimation);
		animationSet.setFillAfter(true);
		animationSet.setFillEnabled(true);
		animationSet.setDuration(POPUP_ANIM_TIME);

		audioLayout.setAnimation(animationSet);
		animationSet.startNow();

		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				audioLayout.hasPopup = true;

				// for clear the animation
				TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0);
				audioLayout.setAnimation(anim);
				audioLayout.clearAnimation();

				setAudioPosition(audioLayout);

				subItemIndex++;
				subItemAnimation();
			}
		});

	}

	private void setAudioPosition(final AudioPlayLayout audioLayout) {

		Drawable drawable = context.getResources()
				.getDrawable(R.drawable.play1);

		Drawable daPause = context.getResources()
				.getDrawable(R.drawable.pause1);

		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				daPause.getIntrinsicWidth(), daPause.getIntrinsicHeight());

		lp.setMargins(audioLayout.ptEnd.x - daPause.getIntrinsicWidth() / 2,
				audioLayout.ptEnd.y - daPause.getIntrinsicHeight() / 2, 0, 0);
		audioLayout.setLayoutParams(lp);

		audioLayout.setTitleVisible(View.VISIBLE);
		this.addView(audioLayout.textTitle);
		ViewTreeObserver vto = audioLayout.textTitle.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			public void onGlobalLayout() {
				audioLayout
						.setTitlePosition(audioLayout.ptEnd.y > audioLayout.ptStart.y);

				audioLayout.textTitle.getViewTreeObserver()
						.removeGlobalOnLayoutListener(this);
			}
		});
	}

	public void disappearAnimation() {
		if (isAnimStart)
			return;

		imagePopBG.setVisibility(View.GONE);
		if (audioLayoutList.size() == 0) {
			popDisappearAnim();
			return;
		}

		Drawable drawable = context.getResources()
				.getDrawable(R.drawable.play1);

		for (int i = 0; i < audioLayoutList.size(); i++) {
			AudioPlayLayout audioLayout = audioLayoutList.get(i);
			audioLayout.clearAnimation();
			audioLayout.setTitleVisible(View.GONE);

			TranslateAnimation tAnimation = new TranslateAnimation(0,
					audioLayout.ptStart.x - audioLayout.ptEnd.x
							+ drawable.getIntrinsicWidth() / 2, 0,
					audioLayout.ptStart.y - audioLayout.ptEnd.y
							+ drawable.getIntrinsicHeight() / 2);

			ScaleAnimation sAnimation = new ScaleAnimation(1, 0, 1, 0);

			AnimationSet animationSet = new AnimationSet(true);
			animationSet.addAnimation(sAnimation);
			animationSet.addAnimation(tAnimation);
			animationSet.setFillEnabled(true);
			animationSet.setFillAfter(true);
			animationSet.setDuration(DISAPPEAR_ANIM_TIME);

			audioLayout.setAnimation(animationSet);
			animationSet.startNow();

			if (i == audioLayoutList.size() - 1) {

				animationSet.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						isAnimStart = true;
					}

					@Override
					public void onAnimationRepeat(Animation animation) {

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						popDisappearAnim();
					}
				});
			}
		}
	}

	private void popDisappearAnim() {

		layoutPopCenter.clearAnimation();

		TranslateAnimation tAnimation = new TranslateAnimation(0, ptPop.x
				- ptDest.x, 0, ptPop.y - ptDest.y);

		ScaleAnimation sAnimation = new ScaleAnimation(1, 0, 1, 0);

		AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(sAnimation);
		animationSet.addAnimation(tAnimation);
		animationSet.setDuration(DISAPPEAR_ANIM_TIME);

		layoutPopCenter.setAnimation(animationSet);
		animationSet.startNow();

		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				isAnimStart = true;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {

				new Handler().post(new Runnable() {

					public void run() {

						ClearSubItem();
						isAnimStart = false;

						Message msg = new Message();
						msg.what = DIS_ANIM_END;
						msgHandler.sendMessage(msg);
					}

				});

			}
		});
	}

	@SuppressLint("NewApi")
	public void ClearSubItem() {

		Log.i("ClearSubItem", "ClearSubItem");

		for (int i = 0; i < audioLayoutList.size(); i++) {
			AudioPlayLayout layout = audioLayoutList.get(i);
			if (layout != null) {
				layout.clearAnimation();
				layout.closeAudio();
				this.removeView(layout.textTitle);
				this.removeView(layout);
				layout = null;
			}
		}

		if (timer != null)
			timer.cancel();

		imagePopBG.setVisibility(View.GONE);
		audioLayoutList.clear();

		layoutPopCenter.clearAnimation();
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(0, 0, 0, 0);
		layoutPopCenter.setLayoutParams(lp);

		this.setVisibility(View.GONE);

		// >= SDK level 11
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			layoutPopCenter.setRotation(0);
			this.setRotation(0);
		}
	}

	public void closeAudio() {
		for (int i = 0; i < audioLayoutList.size(); i++) {
			AudioPlayLayout layout = audioLayoutList.get(i);
			layout.closeAudio();
		}
		imagePopBG.setVisibility(View.GONE);
	}

	private void popShining() {

		final int bgSize = (int) (imagePop.getWidth() * 1.2);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				bgSize, bgSize);
		lp.setMargins(layoutPopCenter.getLeft()
				- (bgSize - imagePop.getWidth()) / 2, layoutPopCenter.getTop()
				- (bgSize - imagePop.getWidth()) / 2, 0, 0);
		imagePopBG.setLayoutParams(lp);

		timer = new Timer();
		bgRadius = bgSize / 2 / 5 * 4;

		TimerTask taskcc = new TimerTask() {

			public void run() {
				if (bgRadius >= bgSize / 2)
					bgRadius = bgSize / 2 / 5 * 4;

				int colorID = getResources().getIdentifier("title_other_color",
						"color", context.getPackageName());
				if (pointObj.typeID == RoutePointObj.TYPE_IN_LINE) {
					int index = (pointObj.index + 1)
							% (SysApplication.POP_NUM / 2);
					colorID = getResources().getIdentifier(
							"title_" + index + "_color", "color",
							context.getPackageName());
				}

				int centerColor = getResources().getColor(colorID);
				int endColor = Color.argb(0, Color.red(centerColor),
						Color.green(centerColor), Color.blue(centerColor));

				imagePopBG.setRadialColor(bgSize / 2, bgSize / 2, bgRadius,
						new int[] { centerColor, centerColor, endColor });

				bgRadius++;
			}
		};

		timer.schedule(taskcc, 50, 50);
	}

	private double getActuralDegree(Point pt, Point ptCenter) {
		double radian = Math.atan2(Math.abs(pt.y - ptCenter.y),
				Math.abs(pt.x - ptCenter.x));
		double degree = Math.toDegrees(radian);
		if (pt.y < ptCenter.y) {

			if (pt.x < ptCenter.x) {
				degree = 180 - degree;
			}

		} else { // bottom of screen
			if (pt.x > ptCenter.x) {
				degree = 360 - degree;
			} else {
				degree = 180 + degree;

			}
		}

		return degree;
	}

	@SuppressLint("NewApi")
	public void repositionPopup(Point pt) {

		double degreeOld2 = getActuralDegree(ptTemp, ptScreenCenter);

		double degreeNew = getActuralDegree(pt, ptScreenCenter);

		if (degreeOld2 != degreeNew) {
			double detaDegrees = degreeOld2 - degreeNew;
			degreeOld += detaDegrees;
			ptTemp = pt;

			PopupLayout.this.setPivotX(ptPop.x);
			PopupLayout.this.setPivotY(ptPop.y);
			PopupLayout.this.setRotation((float) (degreeOld));
			layoutPopCenter.setRotation((float) (-degreeOld));

			for (int i = 0; i < audioLayoutList.size(); i++) {
				AudioPlayLayout audioLayout = audioLayoutList.get(i);
				// Drawable drawable = context.getResources().getDrawable(
				// audioLayout.isPlaying() ? R.drawable.play1 :
				// R.drawable.pause1);
				audioLayout.setPivotX(audioLayout.getWidth() / 2);
				audioLayout.setPivotY(audioLayout.getHeight() / 2);
				audioLayout.setRotation((float) (-degreeOld));

				double degreeAudioOld = audioLayout.degree % 360;
				if (degreeAudioOld < 0)
					degreeAudioOld = 360 + degreeAudioOld;

				double degreeAudio = (audioLayout.degree - detaDegrees) % 360;
				if (degreeAudio < 0)
					degreeAudio = 360 + degreeAudio;

				if (degreeAudioOld < 180 && degreeAudio > 180) {
					audioLayout.setTitlePosition(true);
				} else if (degreeAudioOld > 180 && degreeAudio < 180) {
					audioLayout.setTitlePosition(false);
				}

				audioLayout.degree -= detaDegrees;

				audioLayout.textTitle.setPivotX(audioLayout.textTitle
						.getWidth() / 2);
				audioLayout.textTitle
						.setPivotY(audioLayout.isTitleDown ? -audioLayout
								.getHeight() / 2 + audioLayout.titleTopOffset
								: audioLayout.textTitle.getHeight()
										- audioLayout.titleTopOffset
										+ audioLayout.getHeight() / 2);
				audioLayout.textTitle.setRotation((float) (-degreeOld));

			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth((float) 3.0);

		if (hasPopup && !isAnimStart) {
			canvas.drawLine(ptPop.x, ptPop.y,
					ptDest.x + layoutPopCenter.getWidth() / 2, ptDest.y
							+ layoutPopCenter.getHeight() / 2, paint);
		}

		// for (int i=0; i<audioLayoutList.size(); i++) {
		// AudioPlayLayout audioLayout = audioLayoutList.get(i);
		//
		// if (audioLayout.hasPopup) {
		// canvas.drawLine(audioLayout.ptStart.x, audioLayout.ptStart.y,
		// audioLayout.ptEnd.x,
		// audioLayout.ptEnd.y, paint);
		// }
		// }

	}
}
